home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / bin / exploit.py < prev    next >
Encoding:
Python Source  |  2006-05-22  |  5.9 KB  |  182 lines

  1. from impacket.dcerpc import transport
  2. from impacket import uuid, smb
  3. import random
  4.  
  5. class DCERPCExploit:
  6.     params = {
  7.     # general options
  8.     'host': '192.168.1.1',
  9.     'pipe': 'browser',
  10.     'port': 139,
  11.     'proto': 1,           # 0 UDP, 1 SMB
  12.  
  13.     # SMB options
  14.     'tree_connect': 0,    # 0 = tree_connect, 1 = tree_connect_andx
  15.     'open': 0,            # 0 = open, 1 = open_andx, 2 = nt_create_andx
  16.     'read': 0,            # 0 = read, 1 = read_andx, 2 = read_raw, 3 = read_cycling
  17.     'write': 0,           # 0 = write, 1 = write_andx, 2 = write_raw, 3 = write_cycling
  18.     'transport_frag': -1, # -1 = don't fragment, use TransactNamedPipe.
  19.     'random_offsets': 0,  # randomize offset in write and read requests (when cycling)
  20.     'smb_user': '',
  21.     'smb_passwd': '',
  22.     'smb_lmhash': '',     # lm_hash, first part of pwdump3 output, On of the hashes is enough
  23.     'smb_nthash': '',     # nt_hash, second part of pwdump3 output
  24.  
  25.     # DCERPC options
  26.     'idempotent': 0,      # 
  27.     'dcerpc_frag': -1,    # -1 - don't fragment
  28.     'alter_ctx': 0,       # use alter_ctx instead of bind(). Will issue a bogus bind first
  29.     'bogus_binds': 0,     # number of bogus UUIDs in bind() request
  30.     'bogus_alter': 0,     # number of bogus UUIDs in alter_ctx(), implies alter_ctx
  31.     'endianness': '<',    # < for little endian, > for big endian
  32.                           # When switching to big endian you also need to change the
  33.                   # endianness of the parameters to the function (in dce.call())
  34.                   # Structure does not currently have decent support for this,
  35.                   # specially for the 'w' fields.
  36.     }
  37.  
  38.     UUID = ('01010101-2323-4545-6767-898989898989','1.0')
  39.     BOGUS_UUID = ('12341234-5678-5678-5678-1234567890ab','1.0')
  40.  
  41.     def __init__(self, argv):
  42.     for arg in argv:
  43.         args = arg.split('=',2)
  44.         if len(args) != 2:
  45.             self.usage()
  46.         raise Exception, "Error parsing argument %r" % arg
  47.  
  48.         if len(args) == 1:
  49.         continue
  50.         self.params[args[0]] = args[1]
  51.     
  52.     self.WRITE_TYPE = 0
  53.     self.READ_TYPE  = 0
  54.  
  55.     protocols = (
  56.         'ncadg_ip_udp:%(host)s[%(port)d]',
  57.         'ncacn_np:%(host)s[\\pipe\\%(pipe)s]',
  58.     )
  59.  
  60.     def run(self):
  61.         self.setupConnection()
  62.     self.attackRun()
  63.         
  64.     def open(self, *args):
  65.     args = list(args)
  66.     args[1] = r'\\pipe%s' % args[1]
  67.     args.append(smb.SMB_O_CREAT)
  68.     args.append(smb.SMB_ACCESS_WRITE | smb.SMB_ACCESS_READ)
  69.     return self.smb.open(*args)[0]
  70.     
  71.     def open_andx(self, *args):
  72.     args = list(args)
  73.     args[1] = r'\\pipe%s' % args[1]
  74.     args.append(smb.SMB_O_CREAT)
  75.     args.append(smb.SMB_ACCESS_WRITE | smb.SMB_ACCESS_READ)
  76.     return self.smb.open_andx(*args)[0]
  77.     
  78.     def write_cycling(self, *args, **kargs):
  79.     w = (self.smb.write, self.smb.original_write_andx, self.smb.write_raw)[self.WRITE_TYPE]
  80.     self.WRITE_TYPE += 1
  81.     self.WRITE_TYPE %= 3
  82.     if int(self.params['random_offsets']):
  83.         kargs['offset'] = random.randint(0,65535)
  84.     return w(*args, **kargs)
  85.  
  86.     def read_cycling(self, *args, **kargs):
  87.     w = (self.smb.read, self.smb.original_read_andx, self.smb.read_raw)[self.READ_TYPE]
  88.     self.READ_TYPE += 1
  89.     self.READ_TYPE %= 3
  90.     if int(self.params['random_offsets']):
  91.         kargs['offset'] = random.randint(0,65535)
  92.     return w(*args, **kargs)
  93.  
  94.     def setupConnection(self):
  95.     proto = int(self.params['proto'])
  96.     self.params['port'] = int(self.params['port'])
  97.     
  98.     stringbinding  = self.protocols[proto]
  99.     stringbinding %= self.params
  100.  
  101.     print "Using stringbinding: %r" % stringbinding
  102.  
  103.     self.trans = transport.DCERPCTransportFactory(stringbinding)
  104.     self.trans.set_max_fragment_size(int(self.params['transport_frag']))
  105.     self.trans.set_dport(int(self.params['port']))
  106.  
  107.     try:
  108.         # SMB parameters handling
  109.         self.trans.setup_smb_server()
  110.  
  111.         # force building the SMB object so we can change its methods
  112.         self.smb = self.trans.get_smb_server()
  113.  
  114.         # select the right tree_connect
  115.         arg = int(self.params['tree_connect'])
  116.         if   arg == 0: self.smb.tree_connect_andx = self.smb.tree_connect
  117.         if   arg == 1: self.smb.tree_connect_andx = self.smb.tree_connect_andx
  118.  
  119.         # open selection
  120.         arg = int(self.params['open'])
  121.         if   arg == 0: self.smb.nt_create_andx = self.open
  122.         elif arg == 1: self.smb.nt_create_andx = self.open_andx
  123.  
  124.         # read selection
  125.         arg = int(self.params['read'])
  126.         if   arg == 0: self.smb.read_andx = self.smb.read
  127.         elif arg == 1: self.smb.read_andx = self.smb.read_andx
  128.         elif arg == 2: self.smb.read_andx = self.smb.read_raw
  129.         elif arg == 3:
  130.             self.smb.original_read_andx = self.smb.read_andx
  131.         self.smb.read_andx = self.read_cycling
  132.  
  133.         # write selection
  134.         arg = int(self.params['write'])
  135.         if   arg == 0: self.smb.write_andx = self.smb.write
  136.         elif arg == 1: self.smb.write_andx = self.smb.write_andx
  137.         elif arg == 2: self.smb.write_andx = self.smb.write_raw
  138.         elif arg == 3: 
  139.             self.smb.original_write_andx = self.smb.write_andx
  140.             self.smb.write_andx = self.write_cycling
  141.  
  142.         # smb credentials
  143.         self.trans.set_credentials(
  144.         self.params['smb_user'],
  145.         self.params['smb_passwd'],
  146.         lm_hash = self.params['smb_lmhash'],
  147.         nt_hash = self.params['smb_nthash'])
  148.  
  149.     except Exception, e:
  150.         pass
  151.  
  152.     self.trans.connect()
  153.  
  154.     self.dce = self.trans.DCERPC_class(self.trans)
  155.     self.dce.endianness = self.params['endianness']
  156.  
  157.     # DCERPC parameters handling
  158.     self.dce.set_max_fragment_size(int(self.params['dcerpc_frag']))
  159.     self.dce.set_idempotent(int(self.params['idempotent']))
  160.  
  161.     # alter_ctx
  162.     alter = int(self.params['alter_ctx']) or int(self.params['bogus_alter'])
  163.     if alter:
  164.         _uuid = self.BOGUS_UUID
  165.     else:
  166.         _uuid = self.UUID
  167.     
  168.     # bogus_binds
  169.     self.dce.bind(uuid.uuidtup_to_bin(_uuid), bogus_binds = int(self.params['bogus_binds']))
  170.  
  171.     if proto and alter:
  172.         self.dce = self.dce.alter_ctx(uuid.uuidtup_to_bin(self.UUID), bogus_binds = int(self.params['bogus_alter']))
  173.  
  174.     def usage(self):
  175.         print "Use: python example.py param1=value param2=value2 ..."
  176.     print "see exploit.py to see al available parameters"
  177.     print "for example:\n"
  178.     print "$ python example.py host=192.168.1.1 transport_frag=10"
  179.  
  180.     def attackRun(self):
  181.         pass
  182.